﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Xml;

namespace Web.App_Codes
{
    /// <summary>
    /// 读取IndustryDataMapping配置文件
    /// </summary>
    public class XMLSingletonReader
    {
        private static object initLock = new object();

        private static List<XMLInfo> _xmlInfos = ReadXmlDoc();


        public static List<XMLInfo> GetXMLInfos()
        {
            if (_xmlInfos == null)
            {
                lock (initLock)
                {
                    if (_xmlInfos == null)
                    {
                        _xmlInfos = ReadXmlDoc();
                    }
                }
            }
            return _xmlInfos;
        }

        private static List<XMLInfo> ReadXmlDoc()
        {
            string xmlDocPath = AppDomain.CurrentDomain.BaseDirectory + "IndustryDataMapping.xml";
            List<XMLInfo> xmlInfos = new List<XMLInfo>();
            XmlDocument doc = new XmlDocument();
            doc.Load(xmlDocPath);
            XmlNodeList nodeList = doc.SelectNodes("/IndustryDataMap/IndustryIndexCollect/IndustryIndex");
            if (nodeList != null)
            {
                foreach (XmlNode item in nodeList)
                {
                    XMLInfo info = new XMLInfo();
                    info.Code = item.SelectSingleNode("Code").InnerText;
                    info.Name = item.SelectSingleNode("Name").InnerText;
                    info.PageType = item.SelectSingleNode("PageType").InnerText;
                    info.RecentDataFilter = item.SelectSingleNode("RecentDataFilter").InnerText;
                    info.RecentDataTable = item.SelectSingleNode("RecentDataTable").InnerText;
                    info.RelatedFuture = item.SelectSingleNode("RelatedFuture").InnerText;
                    info.RelatedIndustry = item.SelectSingleNode("RelatedIndustry").InnerText;
                    info.DateColumn = item.SelectSingleNode("DateColumn").InnerText;
                    info.RecentTableType = item.SelectSingleNode("RecentTableType").InnerText;
                    info.Unit = item.SelectSingleNode("Unit").InnerText;

                    List<TabInfo> _tabInfos = new List<TabInfo>();
                    foreach (XmlNode i in item.SelectSingleNode("Tabs").ChildNodes)
                    {
                        TabInfo _tabInfo = new TabInfo();
                        _tabInfo.Code = i.SelectSingleNode("Code").InnerText;
                        _tabInfo.Name = i.SelectSingleNode("Name").InnerText;
                        _tabInfo.PriceColumn = i.SelectSingleNode("PriceColumn").InnerText;
                        _tabInfos.Add(_tabInfo);
                    }
                    info.TabInfos = _tabInfos;

                    List<string> _tableColumns = new List<string>();

                    foreach (XmlNode node in item.SelectSingleNode("RecentTableColumn").ChildNodes)
                    {
                        _tableColumns.Add(node.InnerText);
                    }

                    info.TableColumns = _tableColumns;

                    info.IsAutoGet = Convert.ToBoolean(item.SelectSingleNode("IsAutoGet").InnerText);
                    info.AutoGetFilter = item.SelectSingleNode("AutoGetFilter").InnerText;

                    if (info.IsAutoGet)
                    {
                        List<string> _filters = new List<string>();
                        if (info.RecentDataFilter.EndsWith("="))
                        {
                            //有多个条件
                            if (info.RecentDataFilter.IndexOf(",") > 0)
                            {
                                string _filter = info.RecentDataFilter.Substring(0, info.RecentDataFilter.LastIndexOf(","));
                                string[] temp = _filter.Split(new char[] { ',' });
                                foreach (string s in temp)
                                {
                                    _filters.Add(s);
                                }
                            }

                        }
                        else
                        {
                            string _filter = info.RecentDataFilter.Substring(0, info.RecentDataFilter.LastIndexOf(","));
                            string[] temp = _filter.Split(new char[] { ',' });
                            foreach (string s in temp)
                            {
                                _filters.Add(s);
                            }
                        }
                        if (!string.IsNullOrEmpty(info.AutoGetFilter))
                        {
                            _filters.Add(info.AutoGetFilter);
                        }
                        info.TabInfos = App_Codes.CityRequest.GetCityTabsFromWeb(info.Code, info.RecentDataTable, _filters, GetCount.Count, info.DateColumn, info.TabInfos[0].PriceColumn);
                    }
                    xmlInfos.Add(info);
                }

            }
            else
            {
                log4net.LogManager.GetLogger("david").Error("IndustryDataMapping.xml文件读取失败，请检查文件是否存在");
            }
            return xmlInfos;
        }
    }
}